﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wintellect.PowerCollections;

namespace PriorityQueue
{
    class PriorityQueue<T> : IEnumerable<T>
        where T : IComparable<T>
    {
        private OrderedBag<T> queue;

        public PriorityQueue()
        {
            this.queue = new OrderedBag<T>();
        }

        public void Enqueue(T element)
        {
            queue.Add(element);
        }

        public T Dequeue()
        {
            return queue.RemoveFirst();
        }

        public IEnumerator<T> GetEnumerator()
        {
            for (int i = 0; i < this.queue.Count; i++)
            {
                yield return this.queue[i];
            }
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return ((IEnumerable<T>)this).GetEnumerator();
        }

        public T this [int index]
        {
            get
            {
                if (index >= 0 && index < queue.Count)
                {
                    return queue[index];
                }
                else
                {
                    throw new IndexOutOfRangeException(String.Format("Index {0} is invalid", index));
                }
            }
        }

    }
}
